package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mapper.SettingsMapper;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.db.Settings;
import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void saveMsg(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }
    @Override
    public void head(UserInfo userInfo) {

        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo;
    }

    @Override
    public Map<Long, UserInfo> findIds(List<Long> ids) {
        if(CollUtil.isEmpty(ids)){
            return null;
        }
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserInfo::getId,ids);
        List<UserInfo> userInfos = userInfoMapper.selectList(wrapper);
        Map map = new HashMap<Long,UserInfo>();
        for (UserInfo userInfo : userInfos) {
          map.put(userInfo.getId(),userInfo);
        }
        return map;
    }

    @Override
    public IPage findAll(Integer page, Integer pageSize) {
        Page page1 = new Page<UserInfo>(page, pageSize);
        Page page2 = userInfoMapper.selectPage(page1, null); //分页参数对象，查询条件
        return page2;
    }

}
